﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_UserManager_AddDepartment : PowerCreator.LMS.Web.Base.BasePage
{
    Department opDepartment = new Department();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddDepartment))
        {
            Script.RedirectURL(GetRefUrl());
        }

        if (!IsPostBack)
        {
            InitPage();

            BinderDDL();

            string departmentid = "0";
            if(Request.QueryString["DepartmentID"]!=null)
                departmentid = Request.QueryString["DepartmentID"].ToString();
            ViewState["DepartmentID"] = departmentid;
            ddl_Department.SelectedValue = departmentid;
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddDepartment_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Dep_Name.Text = GetLanageValueByKey("lbl_AddDepartment_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Dep_Sort.Text = GetLanageValueByKey("lbl_AddDepartment_Sort", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Dept_Updept.Text = GetLanageValueByKey("lbl_AddDpt_Updep", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Dep_Remark.Text = GetLanageValueByKey("lbl_AddDepartment_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtnTypeDepartment.Text = GetLanageValueByKey("department_departmenttype1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtnTypeClass.Text = GetLanageValueByKey("department_departmenttype2", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Save.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    //检查页面数据
    private bool CheckPageData()
    {
        if (rdbtnTypeClass.Checked)
        {
            if (ddl_Department.SelectedValue.ToString() != "0")
            {
                VOTDepartment vot = opDepartment.Load(int.Parse(ddl_Department.SelectedValue.ToString()));
                if (vot.DepartmentType == 2)
                {
                    lbl_msg.Text = GetLanageValueByKey("lbl_AddDepartment_departdepartmenttype", PowerCreator.LMS.Language.LanagetItemType.MSG);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                    return false;
                }
            }
        }
        string search = string.Format("DepartmentName='{0}'", txt_DepartmentName.Text);
        if (opDepartment.Count(search) != 0)
        {
            lbl_msg.Text = GetLanageValueByKey("lbl_AddDepartment_departmentName", PowerCreator.LMS.Language.LanagetItemType.MSG);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        return true;
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;

        VOTDepartment vo = new VOTDepartment();
        vo.DepartmentContent = txt_Description.Text;
        vo.DepartmentFatherId = int.Parse(ddl_Department.SelectedValue.ToString());
        vo.DepartmentName = txt_DepartmentName.Text;
        vo.DepartmentType = 1;

        vo.DepartmentLayer = opDepartment.QueryDepartmentLayer(int.Parse(ddl_Department.SelectedValue.ToString()));

        if (rdbtnTypeClass.Checked)
        {
            vo.DepartmentType = 2;
        }
        
        int id = 0;
       
        if (opDepartment.Add(vo,ref id))
        {
            string url = "Department.aspx";
            //if (vo.DepartmentType == 2)
            //{
            //    url = "SetDepartmentAttributes.aspx?DepartmentID=" + id.ToString();
            //}

            Script.RedirectURL(url);
        }
    }

    //绑定分类信息
    private void BinderDDL()
    {
        string search = " DepartmentType=1";
        DataTable dt = opDepartment.Query(search);
        ArrayList list = new ArrayList();
        list =  opDepartment.GetDepartmentTree(dt);
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("department_defaultdeparmentnameroot", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        list.Insert(0, li);

        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "Department.aspx";
        Script.RedirectURL(url);
    }

    protected void rdbtnTypeDepartment_CheckedChanged(object sender, EventArgs e)
    {
        //plDepartment.Visible = true;
        ddl_Department.Enabled = true;
        for (int i = 0; i < ddl_Department.Items.Count; i++)
        {
            if (ddl_Department.Items[i].Value == ViewState["DepartmentID"].ToString())
            {
                ddl_Department.Items[i].Selected = true;
            }
            else
            {
                ddl_Department.Items[i].Selected = false;
            }
        }
     
    }
    protected void rdbtnTypeClass_CheckedChanged(object sender, EventArgs e)
    {
        
        ddl_Department.Enabled = false;
        for (int i = 0; i < ddl_Department.Items.Count; i++)
        {
            if ( i== 0)
            {
                ddl_Department.Items[i].Selected = true;
            }
            else
            {
                ddl_Department.Items[i].Selected = false;
            }
        }
        
        //plDepartment.Visible = false;
    }
}
